package com.toe.forum.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.forum.EnshrineArticleAddDTO;
import com.toe.pojo.dto.forum.EnshrineArticleUpdateDTO;
import com.toe.pojo.entity.forum.UserEnshrineArticle;
import com.toe.forum.webapi.mapper.UserEnshrineArticleMapper;
import com.toe.forum.service.UserEnshrineArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.vo.forum.EnshrineArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户收藏文章表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class UserEnshrineArticleServiceImpl extends ServiceImpl<UserEnshrineArticleMapper, UserEnshrineArticle> implements UserEnshrineArticleService {

    @Autowired
    private UserEnshrineArticleMapper userEnshrineArticleMapper;

    @Override
    public void insert(EnshrineArticleAddDTO enshrineArticleAddDTO) {
        log.info("开始执行添加用户收藏文章映射!!!!");
        //这里也不需要判断此收藏映射是否存在,在ArticleService中会做判断
        Date now = new Date();
        long userId = enshrineArticleAddDTO.getUserId();
        long articleId = enshrineArticleAddDTO.getArticleId();
        UserEnshrineArticle userEnshrineArticle = new UserEnshrineArticle();
        userEnshrineArticle.setArticleId(articleId);
        userEnshrineArticle.setUserId(userId);
        userEnshrineArticle.setCreateTime(now);
        userEnshrineArticle.setUpdateTime(now);
        log.info("插入完成!!!");
        userEnshrineArticleMapper.insert(userEnshrineArticle);
    }

    @Override
    public void deleteById(long id) {
        log.info("开始执行删除用户收藏文章映射");
        //这里不需要判断要删除的记录是否存在
        userEnshrineArticleMapper.deleteById(id);
    }

    @Override
    public void delete(long userId, long articleId) {
        log.info("开始执行删除指定用户收藏文章映射");
        //这里不需要判断要删除的记录是否存在
        userEnshrineArticleMapper.delete(userId,articleId);
    }

    @Override
    public void updateUserEnshrineArticle(EnshrineArticleUpdateDTO enshrineArticleUpdateDTO) {
        log.info("开始执行更新收藏记录模块!!!");
        //此业务几乎用不到
        long user_id = enshrineArticleUpdateDTO.getUserId();
        long article_id = enshrineArticleUpdateDTO.getArticleId();
        EnshrineArticleVO enshrineArticleVO = userEnshrineArticleMapper.selectEnshrine(user_id, article_id);
        if (enshrineArticleVO != null){
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"已存在此记录,不能重复添加!!!");
        }
        log.info("更新完毕!!!");
        userEnshrineArticleMapper.updateUserEnshrineArticle(enshrineArticleUpdateDTO);
    }

    @Override
    public List<EnshrineArticleVO> selectAllEnshrine() {
        log.info("开始执行查询全部收藏记录模块!!!");
        List<EnshrineArticleVO> enshrineArticleVOS = userEnshrineArticleMapper.selectAllEnshrine();
        return enshrineArticleVOS;
    }

    @Override
    public JsonPage<EnshrineArticleVO> selectAllEnshrinePage(Integer pageNum, Integer pageSize) {
        log.info("开始执行分页查询全部收藏记录模块!!!");
        PageHelper.startPage(pageNum,pageSize);
        List<EnshrineArticleVO> enshrineArticleVOS = userEnshrineArticleMapper.selectAllEnshrine();
        return JsonPage.restPage(new PageInfo<>(enshrineArticleVOS));
    }

    @Override
    public List<EnshrineArticleVO> selectAllEnshrineByArticleId(long articleId) {
        log.info("开始执行查询某篇文章的全部收藏记录模块!!!");
        List<EnshrineArticleVO> enshrineArticleVOS = userEnshrineArticleMapper.selectAllEnshrineByArticleId(articleId);
        return enshrineArticleVOS;
    }

    @Override
    public JsonPage<EnshrineArticleVO> selectAllEnshrineByArticleIdPage(long articleId, Integer pageNum, Integer pageSize) {
        log.info("开始执行分页查询某篇文章的全部收藏记录模块!!!");
        PageHelper.startPage(pageNum,pageSize);
        List<EnshrineArticleVO> enshrineArticleVOS = userEnshrineArticleMapper.selectAllEnshrineByArticleId(articleId);
        return JsonPage.restPage(new PageInfo<>(enshrineArticleVOS));
    }

    @Override
    public EnshrineArticleVO selectEnshrineById(long userId,long articleId) {
        log.info("开始执行查询某条收藏记录模块!!!");
        EnshrineArticleVO enshrineArticleVO = userEnshrineArticleMapper.selectEnshrine(userId,articleId);
        return enshrineArticleVO;
    }
}
